package org.csu.mybigpro.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Data;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.csu.mybigpro.common.CommonResponse;
import org.csu.mybigpro.domain.Score;
import org.csu.mybigpro.service.ScoreService;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/score")
@Data
public class ScoreController {
    @Resource
    private ScoreService scoreService;

    @PostMapping("/add")
    public CommonResponse<String> addScore(@RequestBody Score score) {
        scoreService.addScore(score);
        return CommonResponse.createForSuccess("添加成功");
    }

    @GetMapping("/{id}")
    public CommonResponse<Score> getScoreById(@PathVariable Long id) {
        return CommonResponse.createForSuccess(scoreService.getScoreById(id));
    }

    @GetMapping("/list")
    public CommonResponse<List<Score>> getAllScores() {
        return CommonResponse.createForSuccess(scoreService.getAllScores());
    }

    @PutMapping("/update")
    public CommonResponse<String> updateScore(@RequestBody Score score) {
        scoreService.updateScore(score);
        return CommonResponse.createForSuccess("更新成功");
    }

    @DeleteMapping("/delete/{id}")
    public CommonResponse<String> deleteScore(@PathVariable Long id) {
        scoreService.deleteScore(id);
        return CommonResponse.createForSuccess("删除成功");
    }

    @GetMapping("/average")
    public BigDecimal getAverage(@RequestParam Long courseId) {
        return scoreService.getAverageScoreByCourseId(courseId);
    }

    @GetMapping("/distribution")
    public Map<String, BigDecimal> getDistribution(@RequestParam Long courseId) {
        return scoreService.getScoreDistribution(courseId);
    }

    @GetMapping("/export")
    public void exportScores(HttpServletResponse response) throws IOException {
        List<Score> scores = scoreService.getAllScores();  // 你已有的方法

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("成绩表");

        Row header = sheet.createRow(0);
        header.createCell(0).setCellValue("ID");
        header.createCell(1).setCellValue("学生ID");
        header.createCell(2).setCellValue("课程ID");
        header.createCell(3).setCellValue("考试ID");
        header.createCell(4).setCellValue("总分");
        header.createCell(5).setCellValue("等级");

        int rowNum = 1;
        for (Score s : scores) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(s.getId());
            row.createCell(1).setCellValue(s.getStudentId());
            row.createCell(2).setCellValue(s.getCourseId());
            row.createCell(3).setCellValue(s.getExamId());
            row.createCell(4).setCellValue(s.getTotalScore().doubleValue());  // BigDecimal to double
            row.createCell(5).setCellValue(s.getGrade());
        }

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment;filename=scores.xlsx");
        workbook.write(response.getOutputStream());
        workbook.close();
    }

}
